<?php

namespace app\api\model;

use think\Model;
use think\Db;

class ActivityModel extends Model {
    function atvdetail($activityid) {
        $atv = db('activity')->where('activityid', $activityid)->find();
        return $atv;
    }

    function atvlist() {
        $atv = db('activity')->select();
        return $atv;
    }

    function activityenroll($userid, $activityid) {
        $data = [
            'userid' => $userid,
            'activityid' => $activityid,
        ];
        //判断用户存在
        $hasUser = Db::name('user')
            ->where('userid', $userid)
            ->select();

        if (!$hasUser) {
            return ['success' => -1, 'msg' => '无此用户'];
        }

        //判断活动
        $hasActivity = Db::name('activity')
            ->where('activityid', $activityid)
            ->select();

        if (!$hasActivity) {
            return ['success' => -1, 'msg' => '无此活动'];
        }

        //判断报名
        $hasEnroll = Db::name('activity_enroll')
            ->where('userid', $userid)
            ->where('activityid', $activityid)
            ->select();

        if ($hasEnroll) {
            return ['success' => -1, 'msg' => '已报名'];
        }

        $result = Db::name('activity_enroll')->insert($data);
        if ($result) {
            return ['success' => $result];

        } else {
            return ['success' => -1];
        }
    }

    function collect($userid, $activityid) {
        //判断用户存在
        $hasUser = Db::name('user')
            ->where('userid', $userid)
            ->select();

        if (!$hasUser) {
            return ['success' => -1, 'msg' => '无此用户'];
        }

        //判断活动
        $hasActivity = Db::name('activity')
            ->where('activityid', $activityid)
            ->select();

        if (!$hasActivity) {
            return ['success' => -1, 'msg' => '无此活动'];
        }

        //是否已经收藏
        $isCollect = db('activity_follow')
            ->where('activityid', $activityid)
            ->where('userid', $userid)
            ->find();

        if ($isCollect) {
            $res = db('activity_follow')
                ->where('activityid', $activityid)
                ->where('userid', $userid)
                ->delete();
            if ($res) {
                return ['success' => 0, 'msg'=>'取消收藏'];
            } else {
                return ['success' => -1, 'msg'=>'取消收藏失败'];
            }
        } else {
            $data = ['userid' => $userid, 'activityid' => $activityid];
            $res = db('activity_follow')->insert($data);
            if ($res) {
                return ['success' => 1, 'msg'=>'收藏成功'];
            } else {
                return ['success' => -1,'msg'=> '收藏失败'];
            }
        }
    }

    function isCollect($userid, $activityid) {
        $res = db('activity_follow')
            ->where('activityid', $activityid)
            ->where('userid', $userid)
            ->find();
        if ($res) {
            return ['success' => 1];
        } else {
            return ['success' => 0];
        }
    }

/*    function collectList($userid) {
        $res = db('activity_follow')
            ->where('userid',$userid)
            ->select();
        return $res;
    }*/

    function collectActivityList($userid) {
        $res = Db::table('activity_follow')
            ->alias('follow')
            ->join('activity',"follow.activityid = activity.activityid and follow.userid = '$userid' ")
            ->where('userid',$userid)
            ->select();
        return $res;
    }

    function getReply($activityid) {
        $res = db('activity_reply')
            ->where('activityid', $activityid)
            ->select();

        return $res;
    }

    function reply($userid, $activityid,$comment) {
        $data = ['userid'=>$userid,'activityid'=>$activityid,'comment'=>$comment];
        $res = db('activity_reply')
            ->insert($data);
        return ['success' => $res];
    }

//$train_result = Db::table('game_enroll')
//->alias('enroll')
//->join('game',"enroll.gameid = game.gameid and enroll.userid = '$userid' ")
//->select();
//return ['followlist'=> ['activity'=>$activity_result, 'course'=>$course_result, 'game'=>$game_result,'train'=>$train_result]];


}